package com.trigsoft.applist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.trigsoft.utils.BitmapUtil;
import com.trigsoft.utils.PrefUtil;

public class CatDialog implements OnClickListener, OnItemClickListener
{
	Activity context;
	String pkg;
	AlertDialog dialog;
	final ListView lv;
	final ArrayList<String> catsEn;
	ArrayList<String> catsCh;
	ArrayList<Integer> addCatPos = new ArrayList<Integer>();

	public CatDialog(Activity context, String label, String pkg, String cat)
	{
		this.context = context;
		this.pkg = pkg;

		catsCh = new ArrayList<String>(Arrays.asList(context.getResources().getStringArray(R.array.cat)));
		catsCh.remove(0);

		LayoutInflater inflater = context.getLayoutInflater();
		View view = inflater.inflate(R.layout.cat, null);
		((TextView) view.findViewById(R.id.label)).setText(label);
		((ImageButton) view.findViewById(R.id.addButton)).setOnClickListener(this);
		((Button) view.findViewById(R.id.saveButton)).setOnClickListener(this);
		lv = (ListView) view.findViewById(R.id.cats);
		lv.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, catsCh));
		lv.setOnItemClickListener(this);

		String[] tmp = new String[] {"game", "communication", "photo", "entertainments", "transportation", "lifestyle",
				"shopping", "book", "finance", "tool", "education", "sport", "business", "personalization", "other"};
		catsEn = new ArrayList<String>(Arrays.asList(tmp));

		HashSet<String> extraCats = PrefUtil.getAddCat(context);
		catsCh.addAll(extraCats);
		catsEn.addAll(extraCats);

		tmp = cat.split(",");
		for (String s : tmp)
		{
			lv.setItemChecked(catsEn.indexOf(s), true);
		}

		dialog = new AlertDialog.Builder(context).setView(view).create();
	}

	public void show()
	{
		dialog.show();
	}

	public void onClick(View arg0)
	{
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.saveButton)
		{
			DBHelper.updateCat(getListCheckedItemIds(), pkg);
			dialog.dismiss();
			for (Integer p : addCatPos)
			{
				if (lv.isItemChecked(p))
					PrefUtil.addCat(catsCh.get(p), context);
			}
			((AppListAct) context).getCats();
			((AppListAct) context).loadCats(BitmapUtil.isPortrait(context));
			((AppListAct) context).loadData(true);
		} else if (arg0.getId() == R.id.addButton)
		{
			new NewCatDialog(this, context).show();
		}
	}

	protected String getListCheckedItemIds()
	{
		final StringBuilder ids = new StringBuilder();
		boolean firstChecked = true;
		for (int i = 0; i < lv.getCount(); i++)
		{
			if (lv.isItemChecked(i))
			{
				if (!firstChecked)
					ids.append(",");
				//if (i < catsEn.size())
				ids.append(catsEn.get(i));
				//else
				//ids.append(lv.getItemAtPosition(i));
				if (firstChecked)
					firstChecked = false;
			}
		}
		return ids.toString();
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
		// TODO Auto-generated method stub
		int otherPos = catsEn.indexOf("other");
		if (arg2 != otherPos && lv.isItemChecked(arg2))
			lv.setItemChecked(otherPos, false);
		else if (arg2 == otherPos || getListCheckedItemIds().length() == 0)
		{
			for (int i = 0; i < catsEn.size(); i++)
				lv.setItemChecked(i, false);
			lv.setItemChecked(otherPos, true);
		}

	}

	public void addCat(String cat)
	{
		// TODO Auto-generated method stub
		catsCh.add(cat);
		catsEn.add(cat);

		addCatPos.add(catsCh.size() - 1);
		lv.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, catsCh));
		lv.setItemChecked(catsCh.size() - 1, true);
	}
}
